package com.infitecs.eyas.toolbar;

import com.infitecs.eyas.session.SessionService;
import com.infitecs.eyas.toolbar.model.ToolBar;
import com.infitecs.eyas.toolbar.service.ToolBarDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Created by pzw on 2015/8/12.
 */
@Controller
@RequestMapping("/toolbar/")
public class ToolBarController {
    @Autowired
    private ToolBarDataService toolBarDataService;

    @Autowired
    private SessionService sessionService;

    @RequestMapping(value = "airdoor", method = RequestMethod.GET)
    public String airDoor(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/airdoor";
    }

    @RequestMapping(value = "straight", method = RequestMethod.GET)
    public String straight(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/straight";
    }

    @RequestMapping(value = "bend", method = RequestMethod.GET)
    public String bend(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/bend";
    }

    @RequestMapping(value = "header", method = RequestMethod.GET)
    public String header(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/header";
    }

    @RequestMapping(value = "inner-support", method = RequestMethod.GET)
    public String inner(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/inner-support";
    }

    @RequestMapping(value = "threeLinks", method = RequestMethod.GET)
    public String threeLink(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/threeLinks";
    }

    @RequestMapping(value = "transition", method = RequestMethod.GET)
    public String transition(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/transition";
    }

    @RequestMapping(value = "sinkhole", method = RequestMethod.GET)
    public String sinkhole(@RequestParam("id")int id,Model model) {
        sessionService.setEnvironment('T');
        model.addAttribute("data",getData(id));
        model.addAttribute("currentId",id);
        return "toolbar/sinkhole";
    }

    private String getData(int id) {
        ToolBar toolBar = toolBarDataService.getToolBarByCalId(id);
        return toolBar == null ? null : toolBar.getCalValue();
    }

}
